package org.ioriframework.plugin.maven.packaging.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;
import org.ioriframework.plugin.maven.Constants;
import org.ioriframework.plugin.maven.packaging.handlers.CommandHandler;
import org.ioriframework.plugin.maven.util.PreferenceUtils;

/**
 * This is a sample new wizard. Its role is to create a new file 
 * resource in the provided container. If the container resource
 * (a folder or a project) is selected in the workspace 
 * when the wizard is opened, it will accept it as the target
 * container. The wizard creates one file with the extension
 * "mpe". If a sample multi-page editor (also available
 * as a template) is registered for the same extension, it will
 * be able to open it.
 */

public class PackageWizard extends Wizard implements INewWizard {
	private PackageWizardPage page;
	private ISelection selection;

	/**
	 * Constructor for PackageWizard.
	 */
	public PackageWizard() {
		super();
	}
	
	/**
	 * Adding the page to the wizard.
	 */

	public void addPages() {
		page = new PackageWizardPage(selection);
		addPage(page);
	}

	/**
	 * 点finish调用些方法
	 * This method is called when 'Finish' button is pressed in
	 * the wizard. We will create an operation and run it
	 * using wizard as execution context.
	 */
	public boolean performFinish() {
		try {
			CommandHandler handler = new CommandHandler();
			handler.setEnv(page.getEnv());
			handler.setEarTarget(page.getEarTarget());
			handler.setEarFileName(page.getFileName());
			PreferenceUtils.setValue(Constants.STORE_KEY_EAR_TARGET, page.getEarTarget());
			PreferenceUtils.setValue(Constants.STORE_KEY_EAR_FILE_NAME, page.getFileName());
			handler.run();
		} catch (Throwable t) {
			return false;
		}
		return true;
	}

	/**
	 * We will accept the selection in the workbench to see if
	 * we can initialize from it.
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
}